package booyah.application;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

/**
 * 
 */
@Entity
public class Event {

	@Id
	@GeneratedValue
	@Column(name = "EVENT_ID")
	private Long id;

	private String name;

	private String description;

	@ManyToOne
	@JoinColumn(name = "ADMINISTRATOR_ID", nullable = true)
	private Person administrator;

	private String location;

	private Timestamp startTime;

	private Timestamp endTime;

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	// TODO @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
	private List<Position> positions = new ArrayList<Position>();

	public Event() {
	}

	public Event(String name, String description, String location, Timestamp startTime, Timestamp endTime) {
		validateTimes(startTime, endTime);
		this.name = name;
		this.description = description;
		this.location = location;
		this.startTime = startTime;
		this.endTime = endTime;
	}

	public Timestamp getEndTime() {
		return endTime;
	}

	@Deprecated
	public void setEndTime(Timestamp endTime) {
		this.endTime = endTime;
	}

	public Long getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Person getAdministrator() {
		return administrator;
	}

	public void setAdministrator(Person administrator) {
		this.administrator = administrator;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public Timestamp getStartTime() {
		return startTime;
	}

	@Deprecated
	public void setStartTime(Timestamp startTime) {
		this.startTime = startTime;
	}

	public void setTimes(Timestamp startTime, Timestamp endTime) {
		validateTimes(startTime, endTime);
		this.startTime = startTime;
		this.endTime = endTime;
	}
	
	private void validateTimes(Timestamp startTime, Timestamp endTime) {
		if ((startTime == null && endTime != null) || (startTime != null && endTime == null)) {
			throw new IllegalArgumentException("Both Timestamps must be null or non-null");
		}
		if ((startTime != null) && (startTime.after(endTime))) {
			throw new IllegalArgumentException("Starting Timestamp must be earlier than ending Timestamp");
		}
	}
	
	public void addPosition(Position position) {
		positions.add(position);
	}

	public void removePosition(Position position) {
		long targetId = position.getId();
		for (Position p : positions) {
			if (p.getId() == targetId) {
				if (p.getVolunteers().size() != 0) {
					throw new IllegalArgumentException("Position " + targetId + " is not empty.");
				} else {
					positions.remove(p);
				}
			}

		}
	}

	public List<Position> getPositions() {
		return Collections.unmodifiableList(positions);
	}

	public List<Position> getAllFilledPositions() {

		List<Position> filledPositions = new ArrayList<Position>();
		if (positions.size() == 0) {
			return filledPositions;
		}
		for (Position p : positions) {
			if (p.getCount() == p.getVolunteers().size()) {
				filledPositions.add(p);
			}
		}
		return Collections.unmodifiableList(filledPositions);
	}

	public List<Position> getUnfilledPositions() {
		List<Position> unfilledPositions = new ArrayList<Position>();
		if (positions.size() == 0) {
			return unfilledPositions;
		}
		for (Position p : positions) {
			if (p.getCount() > p.getVolunteers().size()) {
				unfilledPositions.add(p);
			}
		}
		return Collections.unmodifiableList(unfilledPositions);
	}

	@Override
	public String toString() {
		String identity = super.toString();
		StringBuffer sb = new StringBuffer(name.length() + identity.length() + 20);
		sb.append("id=");
		sb.append(id.toString());
		sb.append(", ");
		sb.append(name);
		sb.append(" [");
		sb.append(identity);
		sb.append("]");
		return sb.toString();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null || !(obj instanceof Event)) {
			return false;
		}
		Event otherEvent = (Event) obj;
		if ((this == otherEvent) || (id.equals(otherEvent.id))) {
			// TODO Event.equals() override not always true
			// Where two objects have been new'd up but not yet persisted, their
			// identity fields will be equal but they do not represent the same
			// object.
			return true;
		}
		return false;
	}

	@Override
	public int hashCode() {
		return id.toString().hashCode();
	}

}
